﻿Partial Public Class Main

    Public Sub SQLWorkloadPerformance()
        BatchRequests_sec()
        SQLCompilations()
        SQLRecompilations()
        SQLAttentions()
        ActiveCursors()
        SQLErrors()

    End Sub

    Public Sub BatchRequests_sec()

        CalculateCounter("Batch Requests/sec",
                         CStr(IIf(instanceName = "Default", "SQLServer:SQL Statistics", "MSSQL$" + instanceName + ":SQL Statistics")),
                         Me.txtBRsmin,
                         Me.txtBRsmax,
                         Me.txtBRsavg,
                         Me.sparklineBatchRequests)

        'Color text for warning....
        If Me.txtBRsavg.Text <> "0" And Me.txtBRsavg.Text <> String.Empty Then
            If CDbl(txtBRsavg.Text) >= 2500 Then
                Me.txtBRsavg.BackColor = Color.Gold
            End If
        End If

    End Sub

    Private Sub SQLCompilations()

        CalculateCounter("SQL Compilations/sec",
                        CStr(IIf(instanceName = "Default", "SQLServer:SQL Statistics", "MSSQL$" + instanceName + ":SQL Statistics")),
                        Me.txtSCsmin,
                        Me.txtSCsmax,
                        Me.txtSCsavg,
                        Me.sparklineSQLCompilations)

        'Color text for warning....
        If Me.txtBRsavg.Text <> "0" And Me.txtBRsavg.Text <> String.Empty Then
            If (CDbl(Me.txtBRsavg.Text) / 10) < CDbl(Me.txtSCsavg.Text) Then
                Me.txtSCsavg.BackColor = Color.Red
            Else : Me.txtSCsavg.BackColor = Color.LawnGreen
            End If
        End If

    End Sub

    Private Sub SQLRecompilations()
        CalculateCounter("SQL Re-Compilations/sec",
                       CStr(IIf(instanceName = "Default", "SQLServer:SQL Statistics", "MSSQL$" + instanceName + ":SQL Statistics")),
                       Me.txtSRsmin,
                       Me.txtSRsmax,
                       Me.txtSRsavg,
                       Me.sparklineSQLRecompilations)

        'Color text for warning....
        If Me.txtSCsavg.Text <> "0" And Me.txtSCsavg.Text <> String.Empty Then
            If (CDbl(txtSCsavg.Text) / 10) < CDbl(Me.txtSRsavg.Text) Then
                Me.txtSRsavg.BackColor = Color.Red
            Else : Me.txtSRsavg.BackColor = Color.LawnGreen
            End If
        End If

    End Sub

    Private Sub SQLAttentions()
        CalculateCounter("SQL Attention Rate",
                       CStr(IIf(instanceName = "Default", "SQLServer:SQL Statistics", "MSSQL$" + instanceName + ":SQL Statistics")),
                       Me.txtSAsmin,
                       Me.txtSAsmax,
                       Me.txtSAsavg,
                       Me.sparklineSQLAttentions)

        If Me.txtSAsavg.Text <> String.Empty Then
            If (CDbl(txtSAsavg.Text)) > 0 Then
                Me.txtSRsavg.BackColor = Color.Red
            Else : Me.txtSAsavg.BackColor = Color.LawnGreen
            End If
        End If

    End Sub

    Private Sub ActiveCursors()
        CalculateCounter("Active cursors",
                           CStr(IIf(instanceName = "Default", "SQLServer:Cursor Manager by Type", "MSSQL$" + instanceName + ":Cursor Manager by Type")),
                           Me.txtACmin,
                           Me.txtACmax,
                           Me.txtACavg,
                           Me.sparklineActiveCursors,
                           "_Total")

    End Sub

    Private Sub SQLErrors()
        CalculateCounter("Errors/sec",
                       CStr(IIf(instanceName = "Default", "SQLServer:SQL Errors", "MSSQL$" + instanceName + ":SQL Errors")),
                       Me.txtSEsmin,
                       Me.txtSEsmax,
                       Me.txtSEsavg,
                       Me.sparklineSQLErrors,
                       "_Total")

        If Me.txtSEsavg.Text <> String.Empty Then
            If (CDbl(txtSEsavg.Text)) > 0 Then
                Me.txtSEsavg.BackColor = Color.Red
            Else : Me.txtSEsavg.BackColor = Color.LawnGreen
            End If
        End If
    End Sub


End Class
